package com.xiaoshuidi.cloud.module.contract.dal.mysql.contract;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ContractTenantInformationRepVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.MemberContractResp;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.RoomContractRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.company.CompanyRoomDataVO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.RentOrderDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractCompanyDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractRoomDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.RoomContractStatusDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.roommeta.ContractRoommateDO;
import com.xiaoshuidi.cloud.module.contract.service.contract.dto.ContractRoomQueryParamDTO;
import com.xiaoshuidi.cloud.module.contract.vo.ContractRoomMemberUserVO;
import com.xiaoshuidi.cloud.module.contract.vo.ContractRoomReqVO;
import com.xiaoshuidi.cloud.module.contract.vo.ContractRoomReqVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.poi.ss.formula.functions.T;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 租客合同 Mapper
 *
 * @author Alex
 */
@Mapper
public interface ContractRoomMapper extends BaseMapperX<ContractRoomDO> {

    List<RoomContractStatusDO> getDeletedRoomContractByRoomId(Long roomId);

    /**
     * 合同id  查询合同
     * */
    ContractRoomDO getRoomContractById(Long contractId);

    Integer getRenewalContract(Long fromId);

    @Select("select count(id) from contract_room " +
            " where company_contract_id = #{companyContractId} " +
            " and contract_status in ('active','check_in')")
    Integer countContractByCompanyContractId(@Param("companyContractId") Long companyContractId);

    /**
     * 通过企业合同id获取同住人及房间信息
     * @param id
     * @return
     */
    List<ContractTenantInformationRepVO> getRoommateListById(Long id);

    /*
     * 合同id集合查询 企业用户名
     * */
    List<ContractCompanyDO> getCompanyName(List<Long> contractIds);

    /**
     * 是否有未结束的合同
     * */
    List<ContractRoomDO> getRoomContractByRoomId(@Param("roomId") Long roomId);

    @Select("select cre.member_id,  " +
            "       cr.room_name,  " +
            "       cr.contract_no,  " +
            "       cr.start_time,  " +
            "       cr.end_time,  " +
            "       cr.signed_time,  " +
            "       csr.delivery_confirm_time checkInTime,  " +
            "       cre.status,  " +
            "       cre.roommate_type  " +
            "from contract_roommate cre  " +
            "         left join contract_room cr on cre.room_contract_id = cr.id " +
            "left join contract_stay csr on csr.room_contract_id = cr.id "+
            "where ${ew.sqlSegment}" +
            "and cr.contract_status not in('nullify','lost_effectiveness')")
    Page<MemberContractResp> memberContractPage(Page<?> page, @Param("ew") QueryWrapper<?> wrapper);

    @Update("update contract_room set member_id = #{memberId} where id = #{id}")
    int updateMemberIdById(@Param("id")Long id,@Param("memberId")Long memberId);


    @Update("update contract_room set contact_name = #{contactsName},customer_phone = #{phone},agent_phone=#{agentPhone} where  customer_company_id = #{customerCompanyId}")
    int updatePhoneByCustomerCompanyId(@Param("contactsName")String contactsName, @Param("phone")String phone,@Param("agentPhone")String agentPhone,@Param("customerCompanyId")Long customerCompanyId);

    @Update("update contract_room set customer_phone = #{phone} where  customer_company_id = #{customerCompanyId}")
    int updatePhoneByCustomerCompanyId(@Param("phone")String phone,@Param("customerCompanyId")Long customerCompanyId);


    IPage<RoomContractRespVO> getContract(IPage<T> mpPage, @Param("contractDate") LocalDate contractDate,@Param("apartmentId") Long apartmentId);
    Integer getContractCount(@Param("startDate") LocalDate startDate,
                             @Param("endDate") LocalDate endDate);

    List<ContractRoomDO> getFirstOrderUnpaidContractList(@Param("param") ContractRoomQueryParamDTO param);

    List<CompanyRoomDataVO> getCompanyRoomCount(@Param("companyContractId") List<Long> companyContractId);

    List<ContractRoomReqVO> getAllContractByRoomMateUserId(@Param("memberId") Long memberId);

    void updateMemberUser(@Param("userId") Long userId, @Param("mobile") String mobile,
                          @Param("userName") String userName, @Param("oldUserId") Long oldUserId);

    List<ContractRoomDO> getOrderOverdueContractList();
    List<RentOrderDO> getOrderOverList();

    List<ContractRoomMemberUserVO> getRoomNameByMemberIds(@Param("memberIds") List<Long> memberIds);
    void updateOrderAmount(@Param("totalAmount") BigDecimal totalAmount, @Param("orderId")Long orderId);

    List<ContractRoomDO>getExtensionContractList();

    @Select({"SELECT month_rent,room_id FROM contract_room " +
            "WHERE contract_status='active' and NOT (start_time >= #{endTime} or end_time <= #{startTime})"})
    List<ContractRoomDO> queryByHouseType(@Param("startTime") String startTime, @Param("endTime")String endTime);

    @Select({"SELECT month_rent,room_id FROM contract_room WHERE contract_status='active'"})
    List<ContractRoomDO> queryByHouseTypeAll();

    @Select({"select * from contract_room where contract_no=#{orderNo} limit 1"})
    ContractRoomDO queryByOrderNo(String orderNo);
}

